package com.example.springmvcstudy.controller;

import com.example.springmvcstudy.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/req")
public class RequestController {

    //路径变量
    @RequestMapping("/queryStudent/{id}")
    public Object queryStudent(@PathVariable Integer id){
//    public Object queryStudent(@PathVariable("id") Integer studentId){
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return map;
    }

    //获取请求头的信息：通过字段名获取值
    @RequestMapping("/header")
    public Object header(@RequestHeader("User-Agent") String userAgent){
        Map<String, Object> map = new HashMap<>();
        map.put("userAgent", userAgent);
        return map;
    }

    //获取Cookie某个字段：Cookie: 字段名=值(多个)
    @RequestMapping("/cookie")
    public Object cookie(@CookieValue("My-Cookie") String myCookie){
        Map<String, Object> map = new HashMap<>();
        map.put("myCookie", myCookie);
        return map;
    }

    //操作Session：先模拟登录，设置一个用户会话
    @RequestMapping("/login")
    public Object login(HttpServletRequest req){
        //模拟登录成功，登录的用户叫abc
        HttpSession session = req.getSession();//服务端会话管理的数据结构Map.put(随机字符串, session); return session
        session.setAttribute("user", "abc");//session对象也是map对象
        Map<String, Object> map = new HashMap<>();
        map.put("user", "abc");
        return map;
    }
    //操作session：获取session中的abc
    //required = false是配置是否必须的，如果为true，获取不到就会保错
    //如果这个接口，必须登录后访问，则不需要设置required，如果不登录也能访问，就必须设置
    //以上这句话，在学习其他手段后，可以按更加简化的方式配置为不使用required
    @RequestMapping("/getSession")
    public Object getSession(@SessionAttribute(value = "user") String user){
//    public Object getSession(@SessionAttribute(value = "user", required = false) String user){
        Map<String, Object> map = new HashMap<>();
        map.put("session中的user", user);
        return map;
    }

    //===========================================================
    //=== 获取请求数据

    //=== 通过方法参数无注解简单类型
    @RequestMapping("/log")
    public Object log(String username, Integer age, int weight){
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("age", age);
        map.put("weight", weight);
        return map;
    }

    //=== 通过方法参数无注解复杂类型
    @RequestMapping("/complex")
    public Object complex(User user) {
        return user;
    }

    @RequestMapping("/2he1")
    public Object he1(User user, String suibian){
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("suibian", suibian);
        return map;
    }

    @RequestMapping("/rp")
    public Object rp(@RequestParam("usernamexxxxxxxyyy") String username,
                     @RequestParam Integer age,//默认是必须的字段，如果是可选字段，必须配置为required=false
//                     @RequestParam(required = false) Integer age,
                     @RequestParam int weight){
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("age", age);
        map.put("weight", weight);
        return map;
    }

    @RequestMapping("/upload")
    //绑定的是head字段：上传的文件
    public Object upload(@RequestPart MultipartFile head, User user) throws IOException {
        //绑定的是headxxx字段
//    public Object upload(@RequestPart("headxxx") MultipartFile head, User user) throws IOException {
        //上传的文件，保存在服务端本地某个地方：禁止保存在项目目录中，保存在本地其他路径上
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("filename", head.getOriginalFilename());
//        map.put("content", new String(head.getBytes(), "UTF-8"));
        map.put("size", head.getSize());
        head.transferTo(new File("E:/上传的-"+head.getOriginalFilename()));

        return map;
    }

    @RequestMapping("/json")
    public Object json(@RequestBody User user){
        return user;
    }
}
